<?php

namespace app\admin\controller;

use think\Lang;
use think\Db;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Goodscategory extends AdminControl {

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/course.lang.php');
    }


    /**
     * 品类列表
     */
    public function index() {
        $map['pid'] = 0;
        $course_list = Db::name('goods_category')->where($map)->select();
        $this->assign('course_list', $course_list);
        $this->setAdminCurItem('index');
        return $this->fetch('course_list');
    }
    //下架
    public function lower(){
        $category_id = input('get.id');
        //先查询下级有没有上架的
        $map['pid'] = $category_id;
        $course_list = Db::name('goods_category')->where($map)->select();
        foreach ($course_list as $key => $value) {
            if($value['status'] == 0){
                $this->error(lang('ds_category_save_error'));
            }
        }
        $where['id'] = $category_id;
        
        $tag['status'] = 1;
        $course_info = Db::name('goods_category')->where($where)->update($tag);
        
        if ($course_info) {
            $this->success(lang('ds_add_sub_xia_sus'), url('Goodscategory/index'));
        }else {
            $this->error(lang('ds_add_sub_xia_ero'));
        }
    }

    //新增主分类
    public function category_class_add(){
        $this->setAdminCurItem('goods_class_add');
        return $this->fetch('goods_class_add');
    }
    public function category_goods_class_insert(){
        $data = input('post.');
        
        $tag = [
          'category_name' => $data['title'],
          'status'        => $data['status'],
          'create_time'   => time(),
        ];
        $course_list = Db::name('goods_category')->insertGetId($tag);
        if ($course_list) {
            $this->success(lang('ds_category_save_succ'), url('Goodscategory/index'));
        }else {
            $this->error(lang('ds_category_save_fail'));
        }
    }
    //新增二级分类
    public function add_son_category(){
        $category_id = $_GET['id'];
        $this->assign('category_id', $category_id);

        $this->setAdminCurItem('goods_class_add');
        return $this->fetch('course_son_add');
    }
    public function category_son_add(){
        $data = input('post.');
        
        $tag = [
          'category_name' => $data['title'],
          'pid'           => $data['category_id'],
          'status'        => $data['status'],
          'create_time'   => time(),
        ];
        $course_list = Db::name('goods_category')->insertGetId($tag);
        $this->assign('category_id', $data['category_id']);
        if ($course_list) {
            $this->success(lang('ds_category_save_succ'), url('Goodscategory/category_son_list',array('id'=>$data['category_id'])));
        }else {
            $this->error(lang('ds_category_save_fail'));
        }
    }
    //查看下级
    public function category_son_list(){
        $category_id = input('get.id');
        $this->assign('category_id', $category_id);

        $map['pid'] = $category_id;
        
        $course_list = Db::name('goods_category')->where($map)->select();
        foreach($course_list as $k=>$v){
            if($v['status']==1){
                 $course_list[$k]['status']='下架';
            }else{
                $course_list[$k]['status'] = '上架';
            }
        }
        $this->assign('course_list', $course_list);
        $this->setAdminCurItem('index');
        return $this->fetch('course_son_list');
    }
    //主品类编辑
    public function category_class_edit(){
        $category_id = input('get.id');
        $this->assign('category_id', $category_id);
        
        $map['id'] = $category_id;
        $course_info = Db::name('goods_category')->where($map)->find();
        $this->assign('course_info', $course_info);
        return $this->fetch('goods_class_edit');

    }
    public function category_class_insert(){
        $data = input('post.');
        $map['id'] = $data['category_id'];
        $tag = [
          'category_name' => $data['title'],
          'status'        => $data['status']
        ];
        $course_info = Db::name('goods_category')->where($map)->update($tag);
        if ($course_info) {
            $this->success(lang('ds_category_save_succ'), url('Goodscategory/index'));
        }else {
            $this->error(lang('ds_category_save_fail'));
        }
    }

    //二级分类编辑
    public function category_son_edit(){
        $category_id = input('get.id');
        $this->assign('category_id', $category_id);

        $map['id'] = $category_id;
        $course_info = Db::name('goods_category')->where($map)->find();
        $this->assign('course_info', $course_info);
        return $this->fetch('course_son_edit');
    }
    public function category_son_insert(){
        $data = input('post.');
        $map['id'] = $data['category_id'];
        $tag = [
          'category_name' => $data['title'],
          'status'        => $data['status']
        ];
        $course_info = Db::name('goods_category')->where($map)->update($tag);

        //获取pid
        $pid = Db::name('goods_category')->where($map)->find();
        if ($course_info) {
            $this->success(lang('ds_category_save_succ'), url('Goodscategory/category_son_list',array('id'=>$pid['pid'])));
        }else {
            $this->error(lang('ds_category_save_fail'));
        }
    }

    /**
     * 删除
     */
    public function category_class_del() {
        $course_id = input('param.id');
        $result = db('goods_category')->where(['id'=>$course_id])->delete();
         if($result){
            $this->log(lang('ds_del') . 'tag[ID:' . $course_id . ']', 1);
            ds_json_encode('10000', lang('ds_del_suc'));
        } else {
            $this->log(lang('ds_del') . 'tag[ID:' . $course_id . ']', 0);
            ds_json_encode('10001', lang('ds_del_ero'));
        }

    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('goods_category_list'),
                'url' => url('Goodscategory/index')
            ),
        );
      
        $menu_array[] = array(
                'name' => 'goods_class_add',
                'text' => lang('ds_add'),
                'url' => url('Goodscategory/category_class_add')
            );
        return $menu_array;
    }


}

?>
